package com.egg.basic.service.dict.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.egg.basic.entity.dict.DictType;
import com.egg.basic.mapper.dict.DictTypeMapper;
import com.egg.basic.model.request.DictTypePostOrPut;
import com.egg.basic.service.dict.IDictDataService;
import com.egg.basic.service.dict.IDictTypeService;
import com.egg.common.core.exception.CustomException;
import com.egg.common.mybatis.service.impl.BaseServiceImpl;
import com.egg.common.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典类型表 服务实现类
 * </p>
 *
 * @author east
 * @since 2022-06-08
 */
@Slf4j
@Service
public class DictTypeServiceImpl extends BaseServiceImpl<DictTypeMapper, DictType> implements IDictTypeService {


    @Autowired
    private IDictDataService dictDataService;


    private boolean existDictType(String dictType) {
        return existDictType(dictType, Optional.empty());
    }

    private boolean existDictType(String dictType, Optional<Long> filterId) {
        if (ObjectUtil.isEmpty(dictType)) {
            return true;
        }
        LambdaQueryWrapper<DictType> qw = buildLambdaQw();
        qw.eq(DictType::getCode, dictType);
        filterId.ifPresent(o -> qw.notIn(DictType::getId, o));
        return count(qw) > 0;
    }

    /**
     * 新增
     *
     * @param add
     * @return
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public Long add(DictTypePostOrPut add) {
        DictType dictType = JsonUtil.objToObj(add, DictType.class);
        if (existDictType(dictType.getCode())) {
            throw new CustomException("字典类型已存在");
        }
        baseSave(dictType);
        return dictType.getId();
    }

    /**
     * 修改
     *
     * @param id
     * @param edit
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public Long edit(String id, DictTypePostOrPut edit) {
        DictType exist = baseGetById(id);
        DictType info = JsonUtil.objToObj(edit, DictType.class);
        if (existDictType(info.getCode(), Optional.of(exist.getId()))) {
            throw new CustomException("字典类型已存在");
        }
        info.setId(exist.getId());
        baseSave(info);
        return info.getId();
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void del(Collection<? extends Serializable> ids) {
        List<DictType> dictType = baseFindById(ids);
        if (ObjectUtil.isEmpty(dictType)) {
            return;
        }
        baseDel(dictType.stream().map(o -> o.getId()).collect(Collectors.toSet()));
        dictDataService.delByDictTypeCode(dictType.stream().map(o -> o.getCode()).collect(Collectors.toSet()));
    }


}
